package utils;

import java.io.*;

/**
 * @author Limohan
 */
public class DataUtils {
    public static String readData(String fileName) throws IOException {
        StringBuilder sb = new StringBuilder();
        String path = DataUtils.class.getResource("/").getPath()
                + fileName + ".txt";
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String msg = br.readLine();
        while (msg != null) {
            sb.append(msg).append("/");
            msg = br.readLine();
        }
        fr.close();
        br.close();
        return sb.toString();
    }

    public static void writeData(String fileName, String json) throws IOException {
        String path = DataUtils.class.getResource("/").getPath()
                + fileName + ".txt";
        File file = new File(path);
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(json);
        bw.newLine();
        bw.flush();
        bw.close();
    }

    public static void clearData(String fileName) throws IOException {
        String path = DataUtils.class.getResource("/").getPath()
                + fileName + ".txt";
        File file = new File(path);
        FileWriter fw = new FileWriter(file, false);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("");
        bw.flush();
        bw.close();

    }
}
